<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">

<input type="hidden" id="deviceId" th:value="*{deviceId}"/>
<input type="hidden" id="pId" th:value="*{pId}"/>
<input type="hidden" id="byqId" th:value="*{byqId}"/>
<input type="hidden" id="taskStatus" th:value="*{taskStatus}"/>

     <div class="container-div">
		<div class="row">
			<!--<div class="col-sm-12 search-collapse">
				<form id="formId">
					<div class="select-list">
						<ul>
							<li>
								：<input type="text" name="byqId"/>
							</li>

							<li>
								：<input type="text" name="pId"/>
							</li>

							<li>
								设备ID：<input type="text" name="deviceId"/>
							</li>

							<li>
								型号：<input type="text" name="model"/>
							</li>

							<li>
								编号：<input type="text" name="code"/>
							</li>

							<li>
								数据状态：<input type="text" name="datastate"/>
							</li>

							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>-->
			
	        <div class="btn-group-sm" id="toolbar" role="group">
				<a class="btn btn-success" onclick="add()">
					<i class="fa fa-plus"></i> 添加
				</a>
				<a class="btn btn-primary btn-edit disabled" onclick="edit()" >
					<i class="fa fa-edit"></i> 修改
				</a>
				<a class="btn btn-danger btn-del btn-del disabled" onclick="removePdevice()" >
					<i class="fa fa-remove"></i> 删除
				</a>
				<!--<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="assets:kPdKg:export">
						<i class="fa fa-download"></i> 导出
				 </a>-->
			</div>
			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table" data-mobile-responsive="true"></table>
			</div>
		</div>
	</div>
    <div th:include="include :: footer"></div>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('assets:kPdKg:edit')}]];
        var removeFlag = [[${@permission.hasPermi('assets:kPdKg:remove')}]];
        var prefix = ctx + "assets/kPdKg";
		var deviceId = $("#deviceId").val();
		var pId = $("#pId").val();
		var byqId = $("#byqId").val();
		var taskStatus = $("#taskStatus").val();

        var overAllIds = new Array();  //全局数组
        var ids = null;

        $(function() {
            var options = {
                url: prefix + "/listChoosekPdKg?deviceId="+deviceId+"&pId="+pId+"&byqId="+byqId,
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
				exportUrl: prefix + "/export",
                modalName: "配电-开关",
                showSearch: false,	// 是否显示检索信息
                showRefresh: false,	// 是否显示刷新按钮
                showColumns: false,	// 是否显示隐藏某列下拉框
                showToggle: false,	// 是否显示详细视图和列表视图的切换按钮
//		        showExport: true,
                columns: [{
		            checkbox: true
		        },
				{
					field : 'id', 
					title : '',
					visible: false
				},
				{
					field : 'byqId', 
					title : '',
					sortable: true,
                    visible: false
				},
				{
					field : 'pId', 
					title : '',
					sortable: true,
                    visible: false
				},
				{
					field : 'deviceId', 
					title : '设备ID',
					sortable: true,
                    visible: false
				},
				{
					field : 'model', 
					title : '型号',
					sortable: true
				},
				{
					field : 'code', 
					title : '编号',
					sortable: true
				},
				{
					field : 'datastate', 
					title : '数据状态',
					sortable: true,
                    visible: false
				},
		        {
		            title: '操作',
		            align: 'center',
                    visible: false,
		            formatter: function(value, row, index) {
		            	var actions = [];
		            	actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="#" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
						return actions.join('');
		            }
		        }]
            };
            $.table.init(options);


            //	对所选设备添加到数组进行传值到后台添加
            $('#bootstrap-table').on('uncheck.bs.table check.bs.table check-all.bs.table uncheck-all.bs.table',function(e,rows){
                var datas = $.isArray(rows) ? rows : [rows];        // 点击时获取选中的行或取消选中的行
                examine(e.type,datas);                              // 保存到全局 Array() 里
            });



            function examine(type,datas){
                console.log("type=="+JSON.stringify(type))
                if(type.indexOf('uncheck')==-1){
                    console.log("11111")
                    overAllIds.length = 0;
                    console.log("选中状态=="+overAllIds);
                    $.each(datas,function(i,v){
                        // 添加时，判断一行或多行的 id 是否已经在数组里 不存则添加　
                        overAllIds.indexOf(v.id) == -1 ? overAllIds.push(v.id) : -1;
                    });
                }else{
                    console.log("22222")
//                    $.each(datas,function(i,v){
//                        overAllIds.splice(overAllIds.indexOf(v.id),1);    //删除取消选中行
//                    });
                    overAllIds.length = 0;
                }
                console.log("overAllIds0710=="+overAllIds);
                $("#deviceIdArray").val(overAllIds);
                ids = overAllIds;
            }


        });


        function add() {

            if(taskStatus == "归档"){
                return $.modal.alertError("任务已归档，无法添加设备！");
			}

            var url = ctx+"assets/kPdKg/addForDeviceId?deviceId="+deviceId+"&byqId="+byqId+"&pId="+pId;
            $.modal.open("添加开关", url,350,260);
        }
        function edit() {

            if(taskStatus == "归档"){
                return $.modal.alertError("任务已归档，无法修改设备！");
            }

			console.log("电表ID==="+ids)
            var url = ctx+"assets/kPdKg/editForDeviceId/"+ids;
            $.modal.open("修改开关", url,350,260);
        }

        /**
         * 	删除
         */
        function removePdevice() {
            var rows = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);

            // id列
            var ids = $.table.selectFirstColumns();
            //  task_id列
            var taskIds = $.table.selectColumns($.table._option.uniqueId);

            console.log("ids=="+ids+"   taskIds=="+taskIds)

            if(taskStatus == "归档"){
                return $.modal.alertError("任务已归档，无法删除设备！");
            }

            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }



            $.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function () {
                var url = $.table._option.removeUrl;
                var data={"ids":ids.join(),"taskIds":taskIds.join()};
                $.operate.submit(url, "post", "json", data);
            });
        }



    </script>
</body>
</html>